package com.cskaoyan.service.wx;

import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketAddressExample;
import com.cskaoyan.bean.po.MarketCollect;
import com.cskaoyan.bean.po.MarketCollectExample;
import com.cskaoyan.bean.wx.collect.WxCollectListInfoVo;
import com.cskaoyan.bean.wx.collect.WxCollectListVo;
import com.cskaoyan.config.exception.LoginAuthenException;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.mapper.wx.WxCollectMapper;
import com.cskaoyan.util.UserIdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class WxCollectServiceImpl implements WxCollectService{

    @Autowired
    WxCollectMapper wxCollectMapper;

    @Autowired
    MarketCollectMapper marketCollectMapper;

    @Override
    public WxCollectListVo query(BaseParam param,String type) {

        PageHelper.startPage(param.getPage(),param.getLimit());

        WxCollectListVo wxCollectListVo = new WxCollectListVo();

        List<WxCollectListInfoVo>wxCollectListInfoVos = wxCollectMapper.query(type);

        PageInfo pageInfo = new PageInfo(wxCollectListInfoVos);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        wxCollectListVo.setLimit(param.getLimit());
        wxCollectListVo.setPage(param.getPage());
        wxCollectListVo.setTotal((int) total);
        wxCollectListVo.setPages(pages);
        wxCollectListVo.setList(wxCollectListInfoVos);
        return wxCollectListVo;
    }

    @Transactional
    @Override
    public void addordelete(MarketCollect marketCollect) throws LoginAuthenException {
        MarketCollectExample example = new MarketCollectExample();

        MarketCollectExample.Criteria criteria = example.createCriteria();

        criteria.andValueIdEqualTo(marketCollect.getValueId());

        criteria.andTypeEqualTo(marketCollect.getType());

        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(example);

        if(marketCollects.size() == 0){

            Integer userId = UserIdUtil.getUserIdFromSubjectThrowException();
            Date addTime = new Date();
            Boolean deleted = new Boolean(false);
            wxCollectMapper.insert(userId,marketCollect.getValueId(),marketCollect.getType(),addTime,deleted);

        }else {
            MarketCollectExample example2 = new MarketCollectExample();

            MarketCollectExample.Criteria criteria2 = example2.createCriteria();
            criteria2.andValueIdEqualTo(marketCollect.getValueId());

            List<MarketCollect> marketCollects1 = marketCollectMapper.selectByExample(example2);

            for (MarketCollect collect : marketCollects1) {
                marketCollect.setDeleted(collect.getDeleted());
            }

            MarketCollectExample example1 = new MarketCollectExample();

            MarketCollectExample.Criteria criteria1 = example1.createCriteria();

            criteria1.andValueIdEqualTo(marketCollect.getValueId());

            criteria1.andTypeEqualTo(marketCollect.getType());

            marketCollect.setValueId(marketCollect.getValueId());

            marketCollect.setDeleted(!marketCollect.getDeleted());

            marketCollectMapper.updateByExampleSelective(marketCollect,example1);
        }

    }
}
